package cn.yunhe.controller;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.swing.plaf.synth.SynthSpinnerUI;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.yunhe.entity.Drug;
import cn.yunhe.entity.PageNo;
import cn.yunhe.entity.ShuJu;
import cn.yunhe.service.DrugService;
import cn.yunhe.service.ShuJuService;
import cn.yunhe.util.DateUtil;

@Controller
public class DrugController {
	@Autowired
	private DrugService drugService;
	@Autowired
	private ShuJuService shuJuService;

	// 开药
	@RequestMapping("/grtDrug")
	public String selectDrud(HttpServletRequest request) {
		List<Drug> drug = drugService.selectDrug();
		request.setAttribute("drug", drug);
		return "Drug";
	}

	@RequestMapping("/updateDrug")
	public String updateDrud(HttpServletRequest request, int[] id, String num, String usage, String rbname,
			String byname) {
		System.out.println("获取医生名字"+byname);
		if (byname.trim().equals("")){
			request.setAttribute("msg", "请登录,你未登录");
			return "../../login";
		}
			System.out.println("---------------------获取到病人的名字" + rbname);
		List<String> usageList = new ArrayList<>();
		String usa = usage;
		String[] a = usa.split(",");
		for (int i = 0; i < a.length; i++) {
			if (!a[i].equals("")) {
				usageList.add(a[i]);
			}
		}
		for (String string : usageList) {
			System.out.println(string);
		}
		System.out.println("-------------------------1111111111111111-----------------------------------");
		List<String> numList = new ArrayList<>();
		String usa1 = num;
		String[] a1 = usa1.split(",");
		for (int i = 0; i < a1.length; i++) {
			if (!a[i].equals("")) {
				numList.add(a1[i]);
			}
		}
		System.out.println("======================= 增强 for 循环");
		for (String s : numList) {
			System.out.println(s);
		}
		System.out.println("=======================   索引");

		System.out.println("===========2222222222222222222============");
		for (int i = 0; i < id.length; i++) {
			int j = id[i];
			System.out.println(j);
		}
		System.out.println("=======================");
		int sum = 0;
		List<Drug> list2 = new ArrayList<>();
		double zongjia = 0;
		for (int i = 0; i < id.length; i++) {
			int drid = id[i];
			for (int j = 0; j < numList.size(); j++) {
				// string 转 int 类型 在 调用方法
				int numId = Integer.parseInt(numList.get(i));
				System.out.println("numId:::" + numId);
				int num2 = drugService.updateDrugByDrid(drid, numId);
				List<Drug> dlist = drugService.selectDrug2(drid);
				for (Drug drug : dlist) {
					System.out.println(drug.getDrprice() + ".......获取数量单价..........." + numId);
					zongjia = zongjia + (double) (drug.getDrprice() * numId);
					int num222 = shuJuService.insertShuJu(new ShuJu(drug.getDrname(), numId,
							DateUtil.DateToString(new Date()), rbname, byname, drug.getDrprice()));
					System.out.println(num222 + "循环插入值ppppppppppppp");
					System.out.println(zongjia + "22222222222222ddddddddddddddd2222222222");
					list2.addAll(dlist);
					break;
				}
				sum++;
				break;
				// 方法结束 之后 break 即可 ;
			}
			System.out.println("sum::" + sum);
		}
		request.setAttribute("rname", rbname);
		request.setAttribute("zongjia", zongjia);
		request.setAttribute("numId", numList);
		request.setAttribute("list2", list2);
		request.setAttribute("usageList", usageList);
		return "DrugZongjia";
	}

	@RequestMapping("/drugInfo")
	public ModelAndView findDrugAll(PageNo pno, HttpServletRequest request) {
		System.out.println("============药品展示页面!!!!==============");
		ModelAndView mav = new ModelAndView("drugInfo");
		List<Drug> drlist = drugService.findDrygAllByLikeName("%" + "" + "%");
		for (Drug d : drlist) {
			System.out.println(d.getDrname() + "," + d.getSupp().getPname() + "," + d.getType().getTname());
			System.out.println(d.getDrid() + "," + d.getDrstart() + "," + d.getDrstop() + "," + d.getDrnum() + ","
					+ d.getDrprice());
		}
		mav.addObject("dlist", drlist);
		return mav;
	}

	@RequestMapping("/drugPageNo")
	public ModelAndView drugPageNo(PageNo pno, HttpServletRequest request) {
		System.out.println("============分页  药品展示页面!!!!==============");
		ModelAndView mav = new ModelAndView("drugInfo");
		// 调用业务 层 方法逻辑
		PageNo p = drugService.findPageNo(pno);

		List<Drug> drlist = drugService.findDrygPageNoByLikeName("%" + "" + "%", p.getPageNo2(), 5);
		request.setAttribute("dlist", drlist);
		request.setAttribute("page", p.getList());
		request.setAttribute("sum", p.getSum());
		System.out.println("curr_no::" + p.getPageNo() + "num::" + p.getPageNo2() + ",sum::" + p.getSum() + ",start"
				+ p.getStart() + ",end::" + p.getEnd());
		request.setAttribute("curr_no", Integer.parseInt(p.getPageNo()));
		request.setAttribute("totalPageNo", p.getTotalPageNo());
		request.setAttribute("start", Integer.parseInt(p.getStart()));
		request.setAttribute("end", Integer.parseInt(p.getEnd()));

		return mav;
	}

	// 搜索页面
	@RequestMapping("/searchDrug")
	public ModelAndView searchDrugInfo(PageNo pno, HttpServletRequest request) {
		System.out.println("============药品搜索页面!!!!==============");
		ModelAndView mav = new ModelAndView("drugSearch");
		/*
		 * List<Drug> drlist = drugService.findDrygAllByLikeName("%" + drname +
		 * "%"); for (Drug d : drlist) { System.out.println(d.getDrname() + ","
		 * + d.getSupp().getPname() + "," + d.getType().getTname());
		 * System.out.println(d.getDrid() + "," + d.getDrstart() + "," +
		 * d.getDrstop() + "," + d.getDrnum() + "," + d.getDrprice()); }
		 * mav.addObject("dlist", drlist);
		 */
		// 分页 搜索
		PageNo p = drugService.findPageNo(pno);
		List<Drug> drlist = drugService.findDrygPageNoByLikeName("%" + pno.getDrname() + "%", p.getPageNo2(), 5);

		request.setAttribute("dlist", drlist);
		request.setAttribute("page", p.getList());
		request.setAttribute("sum", p.getSum());
		System.out.println("curr_no::" + p.getPageNo() + "num::" + p.getPageNo2() + ",sum::" + p.getSum() + ",start"
				+ p.getStart() + ",end::" + p.getEnd());
		request.setAttribute("curr_no", Integer.parseInt(p.getPageNo()));
		// 搜索的 总数 需要 自己 再 算一下
		request.setAttribute("totalPageNo", pno.getTotalPageNo());
		request.setAttribute("start", Integer.parseInt(p.getStart()));
		request.setAttribute("end", Integer.parseInt(p.getEnd()));
		// 把 所搜的 值 传到前端页面
		System.out.println("drname:::" + pno.getDrname());
		request.setAttribute("drname", pno.getDrname());
		return mav;
	}

	// 修改信息
	@RequestMapping("/updateDrugByDrid")
	public String updateDrugByDrid(int drid, HttpServletRequest request) {
		System.out.println("============药品修改页面1111111!!!!==============");
		Drug drug = drugService.findDrugByDrid(drid);
		request.setAttribute("drug", drug);
		return "drugAlter";
	}

	@RequestMapping("/updateDrugByDrid2")
	public String updateDrugByDrid2(Drug drug) {
		System.out.println("============药品修改页面2222222!!!!==============");
		System.out.println(drug.getDrid() + "," + drug.getDrname() + "," + drug.getDrprice() + "," + drug.getDrstart()
				+ "," + drug.getDrstop());
		System.out.println("," + drug.getType().getTid());
		int num = drugService.updateDrug(drug);
		System.out.println(num + ",,");
		// 重定向

		return "redirect:drugPageNo.do";
	}

	// 删除 一条记录
	@RequestMapping("/deletaDrugByDrstate")
	public String deletaByState(int drid, PageNo pno, HttpServletRequest request) {
		System.out.println("============药品删除 修改状态值!!!!==============");
		int num = drugService.deleteDrugByDrstate(drid);
		System.out.println(num + ",,");
		// 重定向
		return "redirect:drugPageNo.do";
	}

	// 新增药品 drugAdd.do 加 记得 加 状态值
	@RequestMapping("/drugAdd")
	public String drugAdd(Drug drug, PageNo pno, HttpServletRequest request) {
		System.out.println("============药品添加!!!!==============");
		System.out
				.println(drug.getDrname() + "," + drug.getDrprice() + "," + drug.getDrstart() + "," + drug.getDrstop());
		System.out.println("," + drug.getType().getTid());
		drug.setDrstate(1);
		int num = drugService.addDrug(drug);
		System.out.println(num + ",,");
		// 重定向
		return "redirect:drugPageNo.do";
	}

	// 库存
	@RequestMapping("/getDrugDetail")
	public String drugDetail(HttpServletRequest request) {
		System.out.println("============详情添加!!!!==============");
		List<Drug> drlist = drugService.findDrygAllByLikeName("%" + "" + "%");
		request.setAttribute("dlist", drlist);
		return "drugDetail";
	}

	// 剩余时间 (过期时间)
	@RequestMapping("/useDayInstrument")
	public String useDayInstrument(HttpServletRequest request) {
		System.out.println("============详情添加!!!!==============");
		List<Drug> drlist = drugService.findDrugAll();
		List<String> strList = new ArrayList<>();
		for (Drug drug : drlist) {
			Date date2;
			try {
				date2 = DateUtil.StringToDate(drug.getDrstop());
				Date date1 = new Date();
				// 计算差值，分钟数
				long minutes = (date2.getTime() - date1.getTime()) / (1000 * 60);
				System.out.println(minutes);
				// 计算差值，天数
				long days = (date2.getTime() - date1.getTime()) / (1000 * 60 * 60 * 24);
				System.out.println(days);
				// long 类型 转String 类型 或者 ( String.valueOf(longVal) )
				strList.add(Long.toString(days));

			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
		request.setAttribute("dlist", drlist);
		request.setAttribute("stopDate", strList);
		return "drugStopDate";
	}

	public DrugService getDrugService() {
		return drugService;
	}

	public void setDrugService(DrugService drugService) {
		this.drugService = drugService;
	}

	public ShuJuService getShuJuService() {
		return shuJuService;
	}

	public void setShuJuService(ShuJuService shuJuService) {
		this.shuJuService = shuJuService;
	}

}
